package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.CarMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Car;
import com.wanghaixin.pojo.CarExample;
import com.wanghaixin.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 19:43
 * @Description: 车款管理
 */
@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    @Override
    public int addCar(Car car) {
        int i = carMapper.insertSelective(car);
        return i;
    }

    @Override
    public void delCar(Integer id) {
        int i = carMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int upCar(Car car) {
        int update = carMapper.updateByPrimaryKeySelective(car);
        return update;
    }

    @Override
    public Car findById(Integer id) {
        Car car = carMapper.selectByPrimaryKey(id);
        return car;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        CarExample example = new CarExample();
        if (!StringUtils.isEmpty(queryDTO.getSort())) {
            example.setOrderByClause("id");
        }
        List<Car> cars = carMapper.selectByExample(example);
        PageInfo<Car> carPageInfo = new PageInfo<>(cars);
        long total = carPageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, cars);
        return dataGridResult;
    }}
